<?php

namespace app\admin\model;

use think\db\Query;
use think\facade\Db;
use think\Model;

class Category extends Model
{
    public function article()
    {
        return $this->hasMany(Article::class);
    }
    public static function listTree($pid = 0)
    {
        if ($pid == 0) {
            $list = self::where('pid', $pid)->order('sort')->select();
        } else {
            $list = self::where('pid', $pid)->order('sort')->select();
        }
        foreach ($list as $k => $v) {
            $list[$k]['child'] = self::listTree($v['id']);
        }
        return $list;
    }
    public static function listAll($pid = 0, &$list = [], $level = 1)
    {
        if ($pid != '') {
            if ($pid == 0) {
                $cate = self::where(['pid' => 0])->order('sort')->select()->toArray();
            } else {
                $cate = self::where(['pid' => $pid])->order('sort')->select()->toArray();
            }
        }
        if ($pid != 0) {
            $level++;
        }
        foreach ($cate as $k => $v) {
            $v['level'] = $level;
            $list[] = $v;
            if($v['is_show'] == 1){
                self::listAll($v['id'], $list, $level);
            }
            
        }
        return $list;
    }
    public static function list()
    {
        $list = self::listTree();
        $arr = [];
        /* 
        [0]    推荐->2 - cateName
                --电影
                --其他 = cate
                --默认
        [1]       福利->话费
                --1
                --2
        */
        foreach ($list as $k => $v) {
            $first = self::find($v['id'])->category;
            foreach ($v['child'] as $m => $n) {
                $second = self::find($n['id'])->category;
                $key = "[$first]&nbsp&nbsp>>>&nbsp&nbsp[$second]";
                $arr[$k][$m]['category'] = [];
                $arr[$k][$m]['cateName'] = $key;
                foreach ($n['child'] as $i => $l) {
                    $val =  self::find($l['id']);
                    array_push($arr[$k][$m]['category'], ['id' => $val->id, 'cate' => $val->category]);
                }
            }
        }
        return $arr;
    }
    public function getTodayArticleCount($list)
    {
        $todayNum = 0;
        foreach ($list as $k => $j) {
            if (date('d', strtotime($j)) == date('d', time())) $todayNum++;
        }
        return $todayNum;
    }
    // 三级分类数量
    public function threeCount()
    {
        $list = self::select();
        $num = 0;
        foreach ($list as $k => $v) {
            if ($this->isThree($v->id) == 1) {
                $num++;
            }
        }
        return $num;
    }
    // 判断是否为第三级分类
    public function isThree($id)
    {
        $has = $this->findOrEmpty($id)->isEmpty();
        if ($has) return '不存在此分类';
        $data = $this->find($id);
        if ($data['pid'] != 0) {
            $two = $this->find($data['pid']);
            if (!empty($two) && $two['pid'] != 0) {
                return 1;
            } else {
                return '该分类id不符合请求';
            }
        } else {
            return '该分类id不符合请求';
        }
    }
    public function del($id)
    {
        $cateData = $this->find($id);
        if ($cateData['img_path']) {
            $imgPath = app()->getRootPath() . 'public' . $cateData->img_path;
            if (file_exists($imgPath)) {
                unlink($imgPath);
            }
        }
        $res = $this->destroy($id);
        if (!$res) return 0;
        // 三级分类 - 删除全部相对应文章
        if ($this->isThree($id) == 1) {
            $artDataAll = $this->with(['article'])->where(['id' => $id])->find();
            foreach ($artDataAll->article as $k => $v) {
                (new Article())->del($v->id);
                $this->destroy($v->id);
            }
        } else {
            // 一级分类 包含 二级
            if ($cateData->pid == 0) {
                $list = $this->where('pid', $cateData->id)->select();
                foreach ($list as $k => $v) {
                    $this->del($v->id);
                }
            } else {
                // 二级分类
                $threeIdArr = $this->where('pid', $id)->field('id,img_path')->select();
                foreach ($threeIdArr as $k => $v) {
                    $artDataAll = $this->with(['article'])->where(['id' => $v->id])->find();
                    foreach ($artDataAll->article as $m => $n) {
                        (new Article())->del($n->id);
                    }
                    if ($v->img_path) {
                        $imgPath = app()->getRootPath() . 'public' . $v->img_path;
                        if (file_exists($imgPath)) {
                            unlink($imgPath);
                        }
                    }
                    $this->destroy($v->id);
                }
            }
        }
        return 1;
    }
}
